package com.cjh.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.dto.ClerkPagingDTO;
import com.cjh.common.dto.PagingDTO;
import com.cjh.common.dto.StoreClerkUserDTO;
import com.cjh.common.entity.Clerk;
import com.cjh.common.entity.User;
import com.cjh.common.mapper.UserMapper;
import com.cjh.common.response.Result;
import com.cjh.common.service.ClerkService;
import com.cjh.common.mapper.ClerkMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class ClerkServiceImpl extends ServiceImpl<ClerkMapper, Clerk>
    implements ClerkService{

    @Autowired
    ClerkMapper clerkMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PermissionServiceImpl permissionService;

    @Override
    public Result getAllClerk(ClerkPagingDTO clerkPagingDTO) {
        PagingDTO<StoreClerkUserDTO> storeClerkUserDTOPagingDTO = new PagingDTO<>();
        Integer size = clerkPagingDTO.getSize();
        Integer current = clerkPagingDTO.getCurrent();
        Integer start = (current-1)*size;
        String username = clerkPagingDTO.getUsername();
        String dbUserName = clerkPagingDTO.getDbUserName();
        Integer storeId = clerkPagingDTO.getStoreId();
        storeClerkUserDTOPagingDTO.setSize(size);
        storeClerkUserDTOPagingDTO.setCurrent(current);
        Integer userId = null;
        if(!permissionService.isSuperAdministrator(dbUserName)){
            userId = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, dbUserName)).getUserId();
        }
        storeClerkUserDTOPagingDTO.setRecords(clerkMapper.selectListByShopownerId(userId,start,size,username,storeId));
        storeClerkUserDTOPagingDTO.setTotal(clerkMapper.selectCountByShopownerId(userId,username,storeId));
        return new Result().setCode(200).setMessage("操作成功！").setData(storeClerkUserDTOPagingDTO);
    }
    @Override
    public Result addClerk(Clerk clerk) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",clerk.getUserId());
        map.put("store_id",clerk.getStoreId());
        List<Clerk> clerks = clerkMapper.selectByMap(map);
        if(clerks.size() == 0){
            clerkMapper.insert(clerk);
            return new Result().setCode(200).setMessage("新增成功！");
        }else{
            return new Result().setCode(201).setMessage("该用户已经是本店员工！");
        }
    }

    @Override
    public Result deleteClerk(Clerk clerk) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",clerk.getUserId());
        map.put("store_id",clerk.getStoreId());
        clerkMapper.deleteByMap(map);
        return new Result().setCode(200).setMessage("删除成功！");
    }

    @Override
    public Result deleteManyClerk(List<Clerk> clerk) {
        for (Clerk c : clerk){
            deleteClerk(c);
        }
        return new Result().setCode(200).setMessage("删除成功！");
    }
}




